// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import CharacterManager from "../Characters/CharacterManager";
import EquipManager from "../Equips/EquipManager";
import ShopManager from "../Shops/ShopManager";

export enum GiftEvent {
    BUYGIFT_EVENT = "buyGift_event"
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class GiftManager {

    private static _single: GiftManager = null;

    public configArr: any[] = [{ id: 1, price: 6, reward: "0:3006:1:gift_hero" },//类型：id:数量，类型：0英雄，1装备，2钻石，3金币
    { id: 2, price: 30, reward: "1:0:10:gift_equip,2:0:3000:gift_diamond,3:0:100000:gift_gold" },
    { id: 3, price: 68, reward: "1:0:30:gift_equip,2:0:6800:gift_diamond,3:0:300000:gift_gold" },
    { id: 4, price: 128, reward: "1:0:50:gift_equip,2:0:12800:gift_diamond,3:0:500000:gift_gold" }];

    public static get single(): GiftManager {

        if (GiftManager._single == null) {
            GiftManager._single = new GiftManager();
        }
        return GiftManager._single;
    }
    getGiftArr(): boolean[] {
        return SaveManager.single.userdata.giftArr;
    }
    isAllBuyGift(): boolean {
        var arr = this.getGiftArr();
        for (const iterator of arr) {
            if (!iterator) {
                return false;
            }
        }

        return true;
    }

    requestBuy(index: string) {
        var usedata = SaveManager.single.userdata;
        usedata.giftArr[Number(index)] = true;
        var configObj = this.configArr[Number(index)];
        var rewardArr: string[] = configObj.reward.split(",");
        for (const iterator of rewardArr) {
            var arr = iterator.split(":");
            if (arr[0] == "0") {//英雄
                CharacterManager.single.requestUnlockHero(Number(arr[1]));
            } else if (arr[0] == "1") {//随机装备
                for (let i = 0; i < Number(arr[2]); i++) {
                    setTimeout(() => {
                        ShopManager.single.getEquip();
                    }, 50 * i);
                }
            } else if (arr[0] == "2") {//钻石
                usedata.diamond += Number(arr[2]);
                UIManager.single.showTips("钻石 +" + Number(arr[2]));
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
            } else if (arr[0] == "3") {//金币
                usedata.gold += Number(arr[2]);
                UIManager.single.showTips("金币 +" + Number(arr[2]));
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
            }
        }
        SaveManager.single.saveData();
        GameEvent.eventNode.emit(GiftEvent.BUYGIFT_EVENT);
    }


}
